/*
 * dbgout.h
 *
 *	The debug output streambuf. Can be altered to go to any source if the need arises.
 *
 *  Created on: May 7, 2013
 *      Author: zapmunk
 */

#pragma once

#include <iostream>
#include <sstream>
#include <stdio.h>

//This should not be used within the program. Only the 2 global functions at the bottom should be used.
namespace _dout {
	class dbgbuf: public std::stringbuf {
	private:
		static dbgbuf* theDbgbuf;

		std::streambuf* oldStdout;
		std::ostream* oldCout;
		virtual int sync();

		bool output();		//Prints the contents of the buffer to wherever it goes
		void init();		//Initializes the buffer (Spawn console, etc)
		void cleanup();		//Cleans up any detritus the buffer left (remove console, etc)
	public:
		static bool commandeerStdout();	//Commandeers the standard output stream. Cout will now direct to this.
		static bool releaseStdout();	//Releases the standard output stream. Cout will go to its normal place.

		dbgbuf(): oldStdout(NULL), oldCout(NULL) {}
	};
};

static bool (*const commandeerStdout)(void) = _dout::dbgbuf::commandeerStdout;
static bool (*const releaseStdout)(void) = _dout::dbgbuf::releaseStdout;
